package com.vhdadw.config;

import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.scheduling.annotation.EnableAsync;
import org.springframework.scheduling.concurrent.ThreadPoolTaskExecutor;

import java.util.concurrent.ThreadPoolExecutor;

@Configuration
@EnableAsync
public class  ThreadPoolTaskConfig {

    @Bean("threadPoolTaskExecutor")
    public ThreadPoolTaskExecutor threadPoolTaskExecutor(){

        ThreadPoolTaskExecutor executor = new ThreadPoolTaskExecutor();

        //核心线程数，没有任务也保持
        executor.setCorePoolSize(16);
        //阻塞队列最大值
        executor.setQueueCapacity(1024);
        //最大线程数
        executor.setMaxPoolSize(64);
        //线程空闲时间，超时推出，直到等于CorePoolSize
        executor.setKeepAliveSeconds(30);

        executor.setThreadNamePrefix("vhdadw自定义线程池");

        //拒绝策略
        executor.setRejectedExecutionHandler(new ThreadPoolExecutor.AbortPolicy());

        executor.initialize();

        return executor;
    }

}
